import React from 'react';
import {connect}      from 'react-redux';
import {switchNote} from '../actions/note';

@connect(function (state) {
  return {
    items: state.note.items
  }
})
class NoteEmptyPaper extends React.Component {
  static propTypes = {
    name: React.PropTypes.string,
    dispatch: React.PropTypes.func,
  };

  static contextTypes = {
    router: React.PropTypes.object
  };

  constructor(props) {
    super(props);
  }

  componentDidMount() {
    this.props.dispatch(switchNote());
    this.navTo(this.props);
  }

  componentWillReceiveProps(nextProps) {
    this.navTo(nextProps);
  }

  navTo(props) {
    if (props.items.length > 0) {
      this.context.router.push(`/b-${props.params.bookId}/n-${props.items[0]._id}`);
    }
  }

  render() {
    return (
      <div id="NotePaper"/>
    );
  }
}

export default NoteEmptyPaper;